﻿using System;
using System.Collections.Generic;
using System.Text;
using TTEngine.Graphics;
using TTEngine.Resource;

namespace TTEngine.Framework.UI
{
    public class UIFramework : UITarget
    {

        private const string src_smoothpixel = @"#version 300 es
precision highp float;//
precision highp sampler2DArray;

in vec2 vUv;//
in vec4 vColor;//
        
layout(location = 0) out vec4 fragColor;

uniform sampler2D tex;  //
uniform float borderuv;
void main(void) //line: 11
{
    vec2 size = vec2(textureSize(tex,0));//line 13
    vec2 uvsize =size*vUv;
    vec2 subuv = mod(uvsize,1.0);//got subuv
    
    vec2 uvzero = vUv - (subuv)/size;//got zero uv
    vec2 offset = subuv;//center position will be 0.5
    if(offset.x>borderuv &&offset.x<(1.0-borderuv))
        offset.x=0.5;
    if(offset.y>borderuv &&offset.y<(1.0-borderuv))
        offset.y=0.5;
    vec4 texc = texture(tex,uvzero+offset/size);
    vec4 outc = vColor;
    outc = vColor * texc;
    fragColor =  outc;
}";
        public UIFramework(CommonResource Resource)
            : base(API.view, Resource)
        {

        }
        public void Init()
        {
            var vs_simple = Resource.Package.GetNamedResource<ShaderObj>("vs_simple");
            var fs_SmoothPixel = new ShaderObj(Resource.Package, Silk.NET.OpenGLES.ShaderType.FragmentShader, src_smoothpixel);
            var shader = new ShaderProgram(Resource.Package, vs_simple, fs_SmoothPixel);
            shader.SetUniqueName("smoothpixel");

            API.view.OnRender = TargetRender;
            API.view.OnUpdate = TargetUpdate;

            //所有输入事件，都交给UI
            API.view.input.OnPoint = (id, x, y, press, move) =>
            {
                Canvas.OnTouch(null, id, press, move, x, y);
            };
            API.view.input.OnMousePoint = (btn, x, y, press) =>
            {

            };
            API.view.input.OnMouseWheel = (deltaX, deltaY) =>
            {
                Canvas.OnWheel(null, deltaX, deltaY, 0);
            };
            API.view.input.OnKey = (id, key) =>
            {
            };
            API.view.input.OnInputText = (txt) =>
            {

            };

        }

    }
}
